<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
  <meta charset="UTF-8">
  <title>用户列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <link rel="stylesheet" th:href="@{/css/font.css}" href="../static/css/font.css">
  <link rel="stylesheet" th:href="@{/css/xadmin.css}" href="../static/css/xadmin.css">
  <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script type="text/javascript" th:src="@{/lib/layui/layui.js}" src="../static/lib/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" th:src="@{/js/xadmin.js}" src="../static/js/xadmin.js"></script>
  <script type="text/javascript" th:src="@{/js/cookie.js}" src="../static/js/cookie.js"></script>
  <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
  <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>

<body class="">
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
  <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
  <!--<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="user-list.html" title="刷新">-->
    <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
  <div class="layui-row">
    <form class="layui-form layui-col-md12 x-so">
      <input type="text" name="uname"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
      <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
    </form>
  </div>
  <table  class="layui-table" lay-data="{url:'/admin/user/list',page:true,toolbar: '#toolbarDemo',id:'test',limit:10}" lay-filter="test">
    <form>
    <thead>
    <tr>
      <th lay-data="{type:'checkbox'}">ID</th>
      <th lay-data="{field:'uid', width:80, sort: true}">ID</th>
      <th lay-data="{field:'uname', width:200, edit: 'text'}">用户名</th>
      <th lay-data="{field:'upd', edit: 'text', minWidth: 150}">密码</th>
      <th lay-data="{field:'usex', width:80,templet: '#switchTpl'}" >性别</th>
      <th lay-data="{field:'uage', edit: 'text', Width: 30}">年龄</th>
      <th lay-data="{field:'uphone', edit: 'text'}">电话</th>
      <th lay-data="{field:'ubalance',edit: 'text',Width:100}">账户余额</th>
      <th lay-data="{field:'udate' , minWidth: 150}">注册时间</th>
    </tr>
    </thead>
    </form>
  </table>
  <!--方法渲染-->
  <!--<table id="user_table" lay-filter="test" class="layui-table"></table>-->
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-danger" lay-event="delCheckData"><i class="layui-icon"></i>删除选中用户</button>
    <button class="layui-btn layui-btn-sm" onclick="x_admin_show('添加用户','/admin/user/add-user',600,500)"><i class="layui-icon"></i>添加用户</button>
    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
  </div>
</script>
<script type="text/html" id="switchTpl">
  {{#  if (d.usex!='男'){ }}
  <input type="checkbox" name="sex" value="{{d.uid}}" lay-skin="switch" lay-text="女|男" lay-filter="sexDemo" checked>
  {{#  } else { }}
  <input type="checkbox" name="sex"  value="{{d.uid}}" lay-skin="switch" lay-text="女|男" lay-filter="sexDemo" >
  {{#  } }}

</script>

<script>
  layui.use('laydate', function(){
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
      elem: '#start' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#end' //指定元素
    });


  });
</script>
<script >
  layui.use('table', function(){
    var table = layui.table;
    var url;
    var reg = new RegExp("(^|&)" + "uname" + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null){
      if (unescape(r[2])!=""){
        url = "/admin/user/list?"+unescape(r[0]);
        table.reload('test', {
          url: url
        });
      }
    }

//执行渲染
    /*table.render({
      elem: '#user_table' //指定原始表格元素选择器（推荐id选择器）
      ,cols: [
        [
        {type:'checkbox',title:'ID'},
        {field:'uname', width:200, edit: 'text',title: '用户名'},
        {field:'upd', edit: 'text', minWidth: 150,title: '密码'},
        {field:'usex', width:80,title: '性别',templet: '#switchTpl',
          templet: function(data){
          var uid =data.uid;
          if(data.usex=='男'){

            return 	" <form><input type=\"checkbox\" name=\"sex\" id=\"checkSex\" value="+uid+  "  lay-skin=\"switch\" lay-text=\"女|男\" lay-filter=\"sexDemo\" ></form>";
          }else {
            return "  <form><input type=\"checkbox\" name=\"sex\" id=\"checkSex\" value="+uid+ "  lay-skin=\"switch\" lay-text=\"女|男\" lay-filter=\"sexDemo\" checked></form>";
          }
        }
        },
        {field:'uage', edit: 'text', Width: 30,title: '年龄'},
        {field:'uphone', edit: 'text',title: '电话'},
        {field:'ubalance',edit: 'text',Width:100,title: '账户余额'},
        {field:'udate' , minWidth: 150,title: '注册时间'}
        ]


      ] //设置表头
      ,url:'/admin/user/list'
      ,page:true
      ,toolbar: '#toolbarDemo'
      ,id:'test'
      ,limit:10
      //,…… //更多参数参考右侧目录：基本参数选项
    });*/

    var form = layui.form;
    form.on('switch(sexDemo)', function(obj){
      console.log(obj);
      var uid = obj.value;
      var usex;
      if (obj.elem.checked) {
        usex = "女"
      }else {
        usex = "男"
      }
      var data = {
        uid:uid,
        usex:usex
      }
      console.log(data);
      $.ajax({
        type:"post",
        url:"/admin/user/edit",
        data:JSON.stringify(data),
        charset:"utf-8",
        contentType : 'application/json;charset=utf-8',
        dataType: "json",
        success: function(data) {
          if (data.status==200){
            layer.alert(data.message, {icon: 6});
          }else {
            layer.alert(data.message, {icon: 5});
          }
        },
      })


    });
    //监听单元格编辑
    table.on('edit(test)', function(obj){

      var value = obj.value //得到修改后的值
              ,data = obj.data //得到所在行所有键值
              ,field = obj.field; //得到字段
      console.log(data);
      $.ajax({
        type:"post",
        url:"/admin/user/edit",
        data:JSON.stringify(data),
        charset:"utf-8",
        contentType : 'application/json;charset=utf-8',
        dataType: "json",
        success: function(data) {
          if (data.status==200){
            layer.alert(data.message, {icon: 6});
          }else {
            layer.alert(data.message, {icon: 5});
          }
        },
      })
      /*layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);*/
    });

    //头工具栏事件
    table.on('toolbar(test)', function(obj){
      console.log(obj);
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'delCheckData':
          var data = checkStatus.data;
          var ids = [];
          for (let i = 0; i < data.length; i++) {
            ids[i]=data[i].uid;
          }
          console.log(ids)

          layer.confirm('是否删除选中用户?', function(index){
            //do something
            $.ajax({
              type:"post",
              url:"/admin/user/del",
              data:{"uids":ids},
              charset:"utf-8",
              dataType: "json",
              traditional: true,
              success: function(data) {
                if (data.status==200){

                  layer.alert(data.message, {icon: 6},function () {
                    location.replace(location.href);
                  });
                  layer.close(index);
                }else {
                  layer.alert(data.message, {icon: 5},function () {
                    location.replace(location.href);
                  });
                  layer.close(index);
                }
              },
            })

          });
          break;
        case 'getCheckLength':
          var data = checkStatus.data;
          layer.msg('选中了：'+ data.length + ' 个');
          break;
      };
    });


  });
</script>
<script>var _hmt = _hmt || []; (function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>